$(document).ready(function() {
	$("#textareaSendQuestion").click(function() {
		if ($("#submitAsk").css("display") == "none") {
			$(this).val("");
			$(this).css("height", "90px");
			$("#submitAsk").css("display", "inline");
		}
	});
	
	$(".answer").click(function() {
		var button = $(this).next().next();
		var display = $(button).css("display");
		
		if (display == "none") {
			$(this).val("");
			$(this).css("height", "90px");
			$(button).css("display", "inline");
		}
	});
	
	$("#formSendQuestion").validate({
		onkeyup: false,
		onfocusout: false,
		rules: {
			question: {
				required: true,
				maxlength: 500
			}
		},
		messages: {
			question: {
				required: "<br />*請輸入提問內容",
				maxlength: "<br />*提問內容不能超過500字"
			}
		},
		errorPlacement: function(error, element) {
			error.css("color", "red");
			error.css("font-weight", "bold");
			error.insertAfter(element);
		},
		submitHandler: function(form) {
			var content = $("#textareaSendQuestion").val();
			
			$("#submitAsk").attr("disabled", "true");
			$("#textareaSendQuestion").val(content.replace(/\n/g, "<br />"));
			$(form).ajaxSubmit();
			alert("問題已送出");
			location.reload();
		}
	});
	
	$(".ans").each(function() {
		var thisForm = this;
		
		$(this).validate({
			onkeyup: false,
			onfocusout: false,
			rules: {
				content: {
					required: true,
					maxlength: 500
				}
			},
			messages: {
				content: {
					required: "<br />*請輸入回答內容",
					maxlength: "<br />*回答內容不能超過500字"
				}
			},
			errorPlacement: function(error, element) {
				error.css("color", "red");
				error.css("font-weight", "bold");
				error.insertAfter(element);
			},
			submitHandler: function(form) {
				var text = $(thisForm).children("textarea[name=\"content\"]");
				var button = $(text).next().next();
				var content = text.val();
				
				$(button).attr("disabled", "true");
				$(text).val(content.replace(/\n/g, "<br />"));
				$(form).ajaxSubmit();
				alert("回答已送出");
				location.reload();
			}
		});
	});
	
});